using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Chapter10CustomControlsCS
{
    [ToolboxData("<{0}:Provider runat=server></{0}:Provider>")]
    public class ProviderPart : System.Web.UI.WebControls.WebParts.WebPart, IBookInfo,
            System.Web.UI.WebControls.WebParts.IWebPartField, System.Web.UI.WebControls.WebParts.IWebPartParameters
    {
        string MyData = "ISBN Provider: 0130230731";
        string strBookTitle = "Custom Controls and WebParts";
        string strBookAuthor = "Peter Vogel";        
        PropertyDescriptorCollection pcSchema;
        
        string IBookInfo.ISBN        
        {
            get
            {
                return MyData;
            }
            set
            {
                MyData = value;
            }
        }

        string BookTitle
        {
            get
            {
                return strBookTitle;
            }
            set
            {
                strBookTitle = value;
            }
        }

        string BookAuthor
        {
            get
            {
                return strBookAuthor;
            }
            set
            {
                strBookAuthor = value;
            }
        }
        [System.Web.UI.WebControls.WebParts.ConnectionProvider("Provides ISBN")]
        public IBookInfo IBookInfoProvider()
        {
            return this;
        }

        void IBookInfo.UpdateCache(bool CacheFlag)
        {
        //...business logic
        }

         bool IBookInfo.SortData(string Direction)
        {
            return true;
        //...business logic
        }

        void System.Web.UI.WebControls.WebParts.IWebPartField.GetFieldValue(
                                     System.Web.UI.WebControls.WebParts.FieldCallback fld)
        {
            fld.Invoke(this.BookData);
        }

        string BookData
        {
            get
            {
                return strBookTitle;
            }
        }

         System.ComponentModel.PropertyDescriptor System.Web.UI.WebControls.WebParts.IWebPartField.Schema        
        {
            get
            {
                PropertyDescriptorCollection pdc;

                pdc = TypeDescriptor.GetProperties(this);
                return pdc["BookData"];
            }
         }

        [System.Web.UI.WebControls.WebParts.ConnectionProvider("Provides IWebPartField")]
        public System.Web.UI.WebControls.WebParts.IWebPartField IWebPartFieldProvider()
        {
        return this;
        }

        void System.Web.UI.WebControls.WebParts.IWebPartParameters.SetConsumerSchema
                                               (System.ComponentModel.PropertyDescriptorCollection Schema)
        {
            pcSchema = Schema;
        }

        System.ComponentModel.PropertyDescriptorCollection
            System.Web.UI.WebControls.WebParts.IWebPartParameters.Schema
        {
            get
            {
            int ing = 0;
            PropertyDescriptorCollection pdcMe;
            System.ComponentModel.PropertyDescriptor[] pdcReturn; 
            pdcReturn = new System.ComponentModel.PropertyDescriptor[pcSchema.Count];
            
            pdcMe = TypeDescriptor.GetProperties(this);
            foreach(PropertyDescriptor  pd  in pcSchema)
            {
                if (pdcMe[pd.Name] != null)
                {
                 ing += 1;
                 pdcReturn[ing] = pd;
                }
            }

            return new PropertyDescriptorCollection(pdcReturn);
            }
        }
        void System.Web.UI.WebControls.WebParts.IWebPartParameters.GetParametersData
                                    (System.Web.UI.WebControls.WebParts.ParametersCallback prm)
        {
            StateBag dict = new StateBag();

            foreach (PropertyDescriptor prop in pcSchema)
            {
                switch (prop.Name)
                {
                    case "BookTitle":
                        dict.Add("BookTitle", this.BookTitle);
                        break;

                    case "BookAuthor":
                        dict.Add("BookAuthor", this.BookAuthor);
                        break;
                };
            }
            prm.Invoke(dict);
        }
    }
}
